package services.snapchat;

import java.util.Date;
import java.util.HashMap;

import org.json.simple.JSONObject;

public class SnapchatService extends SnapchatAgent {

	private String username;
	private String password;
	
	private int timestamp;
	
	private boolean logged;
	private String auth_token;
	
	public SnapchatService(String u, String p) {
		this.username = u;
		this.password = p;
		
		this.logged = false;
		this.auth_token = null;
	}
	
	public boolean login() {
		timestamp = getNowTimestamp();
		
		HashMap<String, String> data = new HashMap<>();
		data.put("username", username);
		data.put("password", password);
		data.put("timestamp", String.valueOf(timestamp));
		
		JSONObject res = post("/login", data, timestamp, auth_token);
		
		if (res != null && res.containsKey("logged")) {
			logged = (boolean) res.get("logged");
			if (logged) auth_token = (String) res.get("auth_token");
			
			return logged;
		}
		else {
			return false;
		}
	}
	
	private int getNowTimestamp() {
		return (int)(new Date().getTime() / 1000);
	}
	
	
}
